<div class="layuimini-main">
    <div>
    	<div class="layui-form layui-form-pane">
	        <div class="layui-form-item">
	            <label class="layui-form-label">选择应用</label>
	            <div class="layui-input-inline" >
	            	<select lay-filter="appNameFilter" id="appIdse"></select>
	            </div>
	        </div>
	    </div>
    	<div id="bindNode">
        </div>
        <div class="layui-form-item" style="margin-top: 10px">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" id="saveBtn">保存</button>
                <button class="layui-btn layui-btn-danger" id="closeTPage">取消</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'transfer'], function () {
        var transfer = layui.transfer, form = layui.form, layer = layui.layer, $ = layui.$;
        form.render();
        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;
        
        //渲染
        transfer.render({
          elem: '#bindNode',
          title: ['待选择节点', '已选择节点'],
          data: [],
          id: 'bindNodeId'
        })
        let bindNodeInfos={};
    	axios({
    		method:"post",
    		url:"node/queryByNodeInfo",
       		data:{
       			id:""
       		}
    	}).then(function(response){
    		bindNodeInfos = {
        		data: response.data.data,
        		parseData: function(res){
        	        return {
        	            "value": res.id,
        	            "title": res.nodeBName
        	        };
        	    },
        	    value:[]
    		}
    		transfer.reload('bindNodeId',bindNodeInfos);
    	});
    	form.on('select(appNameFilter)', function (data) {
            var appId = data.value;
            if(appId != null && appId !=''){
            	axios({
            		method:"post",
            		url:"app/node/getAppNodeBySomeId",
               		data:{
               			appId:appId
               		}
            	}).then(function(response){
            		var list = response.data.data
            		var value = [];
            		for(var i=0;i<list.length;i++){
            			value.push(list[i].nodeId)
            		}
            		transfer.reload('bindNodeId', {
            			data: bindNodeInfos.data,
            			parseData: bindNodeInfos.parseData,
            			value: value
            		});
            	});
            }else{
            	transfer.reload('bindNodeId',bindNodeInfos);
            }
    	});
    	$("#saveBtn").click(function () {
    		var nodes = transfer.getData('bindNodeId'); // 获取右侧数据
    		var appId = $('#appIdse').val();
    		if(appId == null || appId == ""){
    			layer.msg('请先选择应用');
    			return;
    		}
    		/* if(nodes.length==0){
    			layer.msg('你还未选择任何一个节点');
    			return;
    		} */
  	    	axios({
  	    		method:"post",
  	    		url:"app/node/saveOrEdit",
  	       		data:{
  	       			appId:appId,
  	       			nodes:nodes
  	       		}
  	    	}).then(function(response){
  	    		var data = response.data
        		if(data.success){
        			 layer.close(parentIndex);
        		}else{
        			layer.msg(data.errorMsg, {icon: 2});
        		}
  	    	});
        });
    	$("#closeTPage").click(function () {
        	layer.close(parentIndex);
        });
    });
</script>